package com.deer.wms.base.system.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.model.Area.AreaInfo;
import com.deer.wms.base.system.model.Cell.CellInfo;
import com.deer.wms.base.system.model.Shelf.ShelfCell;
import com.deer.wms.base.system.model.Shelf.ShelfInfo;
import com.deer.wms.base.system.model.Shelf.ShelfInfoCriteria;
import com.deer.wms.base.system.model.Shelf.ShelfInfoDto;
import com.deer.wms.base.system.service.AreaInfoService;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.base.system.service.ShelfInfoService;
import com.deer.wms.busine.tactic.model.PrintTempInfo;
import com.deer.wms.busine.tactic.service.PrintTempInfoService;
import com.deer.wms.busine.tactic.service.PrintTempUtils;
import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.BeanUtils;
import com.deer.wms.project.root.util.MyUtils;
import com.deer.wms.system.manage.model.CellLed;
import com.deer.wms.system.manage.service.CellLedService;
import com.deer.wms.system.manage.service.WareInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guo on 2019/11/27.
 */
@Api(description = "货架信息api接口")
@RestController
@RequestMapping("/shelf/infos")
public class ShelfInfoController {

    @Autowired
    private ShelfInfoService shelfInfoService;

    @Autowired
    private CellInfoService cellInfoService;

    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private WareInfoService wareInfoService;

    @PostMapping("/insert")
    public Result add(@RequestBody ShelfInfo shelfInfo) {
        shelfInfoService.save(shelfInfo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/autoInsert")
    @ApiOperation(value = "添加货架信息", notes = "添加货架信息")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    public Result autoInsert(@RequestBody ShelfCell shelfCell, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        ShelfInfo shelfInfo = new ShelfInfo();

        BeanUtils.copyProperties(shelfCell, shelfInfo);

//        AreaInfo areaInfo = areaInfoService.getById(shelfCell.getAreaId());

        String wareFeel = shelfCell.getShelfCode();
//        String areaFeel = areaInfo.getAreaCode();


        shelfInfoService.save(shelfInfo);
        Integer shelfRow = shelfInfo.getShelfRow();
        Integer shelfColumn = shelfInfo.getShelfColumn();
        for (int i = 1; i <= shelfRow; i++) {
            for (int j = 1; j <= shelfColumn; j++) {

                String one = j <= 9 ? "0" + j : "" + j;
//                String two = i <= 9 ? "0" + i : "" + i;
//                String cellCode = areaFeel + "-" + wareFeel + "-" + two + "-" + one;
                String cellCode = wareFeel + one;
                CellInfo cellInfo = new CellInfo();
                BeanUtils.copyProperties(shelfCell, cellInfo);
                cellInfo.setCellCode(cellCode);
                cellInfo.setCellName(cellCode);
                cellInfo.setShelfInfoId(shelfInfo.getShelfInfoId());
                cellInfo.setsColumn(j);
                cellInfo.setsRow(i);
                cellInfo.setState("enable");
                cellInfo.setyCoordinate(i);
                cellInfo.setxCoordinate(j);

                cellInfoService.save(cellInfo);
            }
        }
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping("/autoInsert2")
    @ApiOperation(value = "添加货架信息", notes = "添加货架信息")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    public Result autoInsert2(@ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        int c;

//        for(c = 'A'; c <= 'Z'; ++c){
        for(c = 1; c <= 7; ++c){
            System.out.print(c + " ");

            ShelfInfo shelfInfo = new ShelfInfo();
            shelfInfo.setShelfCode("20"+c);
            shelfInfo.setShelfName("电子货架"+c);
            shelfInfo.setShelfDescribe("电子货架"+c);
            shelfInfo.setType(2);
            shelfInfo.setAreaId(2);

            shelfInfoService.save(shelfInfo);

            String wareFeel = shelfInfo.getShelfCode();
            for (int j = 1; j <= 1400; j++) {

                String one = MyUtils.formatZeroNumber(j);
                String cellCode = wareFeel + one;
                CellInfo cellInfo = new CellInfo();
                BeanUtils.copyProperties(shelfInfo, cellInfo);
                cellInfo.setCellCode(cellCode);
                cellInfo.setCellName(cellCode);
                cellInfo.setShelfInfoId(shelfInfo.getShelfInfoId());
                cellInfo.setsColumn(j);
                cellInfo.setsRow(1);
                cellInfo.setState("enable");
                cellInfo.setyCoordinate(1);
                cellInfo.setxCoordinate(j);

                cellInfoService.save(cellInfo);
            }
        }

        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/autoInsert3")
    @ApiOperation(value = "卓品的电子料架", notes = "添加货架信息")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    public Result autoInsert3(@ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        QueryWrapper<CellInfo> qw = new QueryWrapper<>();
        qw.ge("shelf_info_id", 56).le("shelf_info_id", 65);
        List<CellInfo> cellInfoList = cellInfoService.list(qw);
        //System.out.println(cellInfoList.size());

        List<CellLed> ledList = new ArrayList<>();
        for (CellInfo cellInfo : cellInfoList) {
            CellLed cellLed = new CellLed();
            cellLed.setCellCode(cellInfo.getCellCode());
            cellLed.setLedId(cellInfo.getsColumn());
//            cellLed.setMac();
//            cellLed.setIp();
            ledList.add(cellLed);
        }
        cellLedService.saveBatch(ledList);
        System.out.println("success");
        return ResultGenerator.genSuccessResult();
    }
    @Autowired
    private CellLedService cellLedService;


    @GetMapping("/delete")
    public Result delete(Integer id) {
        Integer shelfInfoId = id;
//        List<CellInfo> list = cellInfoService.findByShelfInfoId(shelfInfoId);

        cellInfoService.deleteByShelfId(shelfInfoId);
        shelfInfoService.removeById(shelfInfoId);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody ShelfInfo shelfInfo) {


        shelfInfoService.updateById(shelfInfo);
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/autoUpdate")
    @ApiOperation(value = "更新货架信息", notes = "更新货架信息")
    public Result update(@RequestBody ShelfCell shelfCell, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        cellInfoService.deleteByShelfId(shelfCell.getShelfInfoId());
        ShelfInfo shelfInfo = shelfInfoService.getById(shelfCell.getShelfInfoId());
        BeanUtils.copyProperties(shelfCell, shelfInfo);
        shelfInfoService.updateById(shelfInfo);
        AreaInfo areaInfo = areaInfoService.getById(shelfInfo.getAreaId());
        String wareFeel = shelfInfo.getShelfCode();
        String areaFeel = areaInfo.getAreaCode();


        Integer shelfRow = shelfInfo.getShelfRow();
        Integer shelfColumn = shelfInfo.getShelfColumn();
        for (int i = 1; i <= shelfRow; i++) {
            for (int j = 1; j <= shelfColumn; j++) {
                CellInfo cellInfo = new CellInfo();
                BeanUtils.copyProperties(shelfCell, cellInfo);

                String one = j <= 9 ? "0" + j : "" + j;
                String two = i <= 9 ? "0" + i : "" + i;
//                String three = shelfNo <= 9 ? "0" + shelfNo : "" + shelfNo;
                String cellCode = areaFeel + "-" + wareFeel + "-" + two + "-" + one;
                cellInfo.setCellCode(cellCode);
                cellInfo.setShelfInfoId(shelfInfo.getShelfInfoId());
                cellInfo.setsColumn(j);
                cellInfo.setsRow(i);
                cellInfoService.save(cellInfo);
            }
        }
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        ShelfInfo shelfInfo = shelfInfoService.getById(id);
        return ResultGenerator.genSuccessResult(shelfInfo);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/list")
    public Result list(ShelfInfoCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<ShelfInfoDto> list = shelfInfoService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Autowired
    private PrintTempInfoService printTempInfoService;

    /**
     * 打印
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "打印", notes = "打印")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Authority
    @GetMapping("/print")
    public Result print(Integer id, Integer wareId, @ApiIgnore @User CurrentUser currentUser) {


        ShelfInfo shelfInfo = shelfInfoService.getById(id);


        PrintTempInfo printTempInfo = printTempInfoService.findByWare(wareId, "货位组标签");
        if (printTempInfo == null) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "未匹配到货位标签模板");
        }
        String html = printTempInfo.getTempHtml();
        Document doc = Jsoup.parse(html);
        //删除不显示的
        doc.select(".dis-none").remove();
        //直线中间蓝点的问题
        doc.select(".line-handle").remove();


        PrintTempUtils.setTempData("SHELF_CODE", shelfInfo.getShelfCode(), doc, "text");
        PrintTempUtils.setTempData("SHELF_CODE_QR_CODE", shelfInfo.getShelfCode(), doc, "qrCode");
        PrintTempUtils.setTempData("SHELF_CODE_BAR_CODE", shelfInfo.getShelfCode(), doc, "barCode");


        //doc在创建时里面包含了html head body等节点  不需要
        Element labelContent = doc.getElementsByAttributeValue("class", "label-content").get(0);
        String tempHtml = labelContent.toString();


        return ResultGenerator.genSuccessResult(tempHtml);
    }


}
